#!/bin/bash

judgeDeal(){
    last=$1
    curr=$2
    type=$3
    logMark=$4
    if [ $last -eq $curr ]
      then
         echo "$(date) : $logMark  last:$last"
	 ps aux|grep crawlerclient | grep type=$type | awk '{print $2}'|xargs kill -9
         nohup java -jar target/CrawlerClientCAS-1.0.jar type=$type name=crawlerclient project=3 >> "$logMark.log" &
      else 
	 echo "$(date) : $logMark   last:$last   curr:$curr"
      fi
}


last_newss=$(cat newss.log   | grep "保存完成"     | wc | awk '{print $1}')
last_newsm=$(cat newsm.log   | grep "保存完成"     | wc | awk '{print $1}')
last_bbss=$(cat bbss.log     | grep "保存完成"     | wc | awk '{print $1}')
last_weixins=$(cat weixins.log | grep "保存完成"     | wc | awk '{print $1}')
last_weibos=$(cat weibos.log  | grep "新数据已保存" | wc | awk '{print $1}')
while true
  do
      sleep 6h

      curr_newss=$(cat newss.log   | grep "保存完成"     | wc | awk '{print $1}')
      curr_newsm=$(cat newsm.log   | grep "保存完成"     | wc | awk '{print $1}')
      curr_bbss=$(cat bbss.log     | grep "保存完成"     | wc | awk '{print $1}')
      curr_weixins=$(cat weixins.log | grep "保存完成"     | wc | awk '{print $1}')
      curr_weibos=$(cat weibos.log  | grep "新数据已保存" | wc | awk '{print $1}')
      
      judgeDeal $last_newss   $curr_newss   1  "newss"
      judgeDeal $last_newsm   $curr_newsm   2  "newsm"
      judgeDeal $last_bbss    $curr_bbss    3  "bbss"
      judgeDeal $last_weibos  $curr_weibos  7  "weibos"
      judgeDeal $last_weixins $curr_weixins 15 "weixins"
      
      last_newss=$curr_newss
      last_newsm=$curr_newsm
      last_bbss=$curr_bbss
      last_weixins=$curr_weixins
      last_weibos=$curr_weibos

  done
